package cn.yitun.chat.yitunserverimapicore.business.group.action;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.Info;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yitungeneral.annotation.action.ActionMapping;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupJoinApplyDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupJoinApplyVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupJoinVerifyAnswerVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.system.user.UserVo;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupJoinService;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupJoinSettingService;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupJoinVerifyAnswerService;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupManageService;
import cn.yitun.chat.yitunserverimapicore.business.user.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * Date 2019-01-20 20:49:52<br>
 * Description
 * 
 * @author yitun
 * @since 1.0.0
 */
@Component
@ActionMapping(value = ActionTypeConstant.GROUP_JOIN_ACTION_PUSH)
public class GroupJoinAction {

	@Resource
	private GroupJoinSettingService groupJoinSettingService;
	@Resource
	private GroupJoinService groupJoinService;
	@Resource
	private GroupManageService groupManageService;
	@Resource
	private UserService userService;
	@Resource
	private GroupJoinVerifyAnswerService groupJoinVerifyAnswerService;

	/**
	 * Date 2019-01-27 10:39:08<br>
	 * Description 群主/管理员获取申请加入数量
	 * @author yitun
	 * @param socketSession
	 * @return
	 * @since 1.0.0
	 */

	@ActionMapping(value = "1.1.0001")
	public ResultMessage queryJoinApplyReceiveCount(SocketSession socketSession, @JsonDefine("data") GroupJoinApplyDto query) {
		query.setCurrentUserId(socketSession.getKey());
		return new ResultMessage(R.param("count", groupManageService.countJoinRequest(query)));
	}

	/**
	 * Date 2019-01-27 10:39:08<br>
	 * Description 群主/管理员获取申请加入列表
	 * @author yitun
	 * @param socketSession
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0002")
	public ResultMessage queryJoinApplyReceiveList(
			SocketSession socketSession,
			@JsonDefine("data") GroupJoinApplyDto query) {
		Long userId = socketSession.getKey();

		List<Long> userIds = new ArrayList<>();
		List<Long> applyIds = new ArrayList<>();
		List<Long> groupIds = new ArrayList<>();
		query.setCurrentUserId(userId);
		PageHelper.startPage(query.getCurrentPageNo(), query.getPageSize());
		List<GroupJoinApplyVo> applys = groupManageService.queryJoinRequestList(query);

		for (GroupJoinApplyVo ap : applys) {
			userIds.add(ap.getApplyUserId());
			groupIds.add(ap.getGroupId());
			applyIds.add(ap.getId());
		}
		Map<Long, UserVo> userMap = new HashMap<>();
		Map<Long, List<GroupJoinVerifyAnswerVo>> answerMap = new HashMap<>();

		List<UserVo> users = userService.getUserDataList(userIds);
		List<GroupJoinVerifyAnswerVo> answers = groupJoinVerifyAnswerService.getListByApplyIds(groupIds, applyIds);

		for (UserVo u : users) {
			userMap.put(u.getId(), u);
		}

		for (GroupJoinVerifyAnswerVo a : answers) {
			Long aid = a.getApplyId();
			List<GroupJoinVerifyAnswerVo> list = answerMap.get(aid);
			if (null == list) {
				list = new ArrayList<>();
				answerMap.put(aid, list);
			}
			list.add(a);
		}

		List<Map<String, Object>> list = new ArrayList<>();

		for (GroupJoinApplyVo ap : applys) {
			Long aid = ap.getId();
			Long uid = ap.getApplyUserId();
			Map<String, Object> map = new HashMap<>();

			UserVo user = userMap.get(uid);
			List<GroupJoinVerifyAnswerVo> answerList = answerMap.get(aid);
			map.put("apply", ap);
			map.put("user", user);
			map.put("answerList", answerList);

			list.add(map);
		}

		return new ResultMessage(R.param("page", new PageInfo<>(list)));
	}
	/**
	 * Date 2019-01-27 10:39:08<br>
	 * Description 群主/管理员获取申请加入列表
	 *
	 * @author yitun
	 * @param socketSession
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0003")
	public ResultMessage queryJoinApplyDataReceiveList(
			SocketSession socketSession,
			@JsonDefine("data") GroupJoinApplyDto query) {

		query.setCurrentUserId(socketSession.getKey());
		PageHelper.startPage(query.getCurrentPageNo(), query.getPageSize());
		List<GroupJoinApplyVo> applys = groupManageService.queryJoinRequestList(query);

		return new ResultMessage(R.param("page", new PageInfo<>(applys)));
	}

	/**
	 * 
	 * Date 2019-01-26 14:44:23<br>
	 * Description 处理加入申请
	 * 
	 * @author yitun
	 * @param socketSession
	 * @param handle
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0005")
	public ResultMessage joinHandle(
			SocketSession socketSession,
			@JsonDefine("data") GroupJoinApplyDto handle) {
		// String applyId;// 请求记录id
		// String applyUserId;// 添加联系人的请求用户
		// String groupId;// 被添加的用户
		// String handleType;// 处理结果：0:未处理 1:同意 2:拒绝 3:忽略
		// String message;// 附加消息
		handle.setHandleUserId(socketSession.getKey());
		handle.setCurrentUserId(socketSession.getKey());
		Info info = groupJoinService.joinHandle(handle);
		ResultMessage rm = new ResultMessage();
		rm.setInfo(info);
		return rm;
	}

	/**
	 * 
	 * Date 2019-01-26 14:43:22<br>
	 * Description 申请加入
	 * 
	 * @author yitun
	 * @param socketSession
	 * @param request
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0006")
	public ResultMessage joinApply(
			SocketSession socketSession,
			@JsonDefine("data") GroupJoinApplyDto request) {

		request.setApplyUserId(socketSession.getKey());
		request.setCurrentUserId(socketSession.getKey());
		Info info = groupJoinService.joinApply(request, request.getAnswerList());
		ResultMessage message = new ResultMessage();
		message.setInfo(info);
		return message;
	}
}
